package com.zlp.mall.portal.modules.ums.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.RespCode;
import com.zlp.mall.common.exception.CustomException;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.common.util.MethodUtil;
import com.zlp.mall.portal.config.SessionMap;
import com.zlp.mall.portal.modules.ums.dto.req.MemberReceiveAddressAddReq;
import com.zlp.mall.portal.modules.ums.dto.req.MemberReceiveAddressUpdReq;
import com.zlp.mall.portal.modules.ums.mapper.MemberReceiveAddressMapper;
import com.zlp.mall.portal.modules.ums.model.Member;
import com.zlp.mall.portal.modules.ums.model.MemberReceiveAddress;
import com.zlp.mall.portal.modules.ums.service.MemberReceiveAddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 会员收货地址表 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-16
 */
@Service
@RequiredArgsConstructor
@Slf4j(topic = "MemberReceiveAddressServiceImpl")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddress> implements MemberReceiveAddressService {

    private final SessionMap sessionMap;
    
    @Override
    public boolean add(MemberReceiveAddressAddReq memberReceiveAddressAddReq) {

        log.info("add.req memberReceiveAddressAddReq={}", JSON.toJSONString(memberReceiveAddressAddReq));
        MemberReceiveAddress memberReceiveAddress = new MemberReceiveAddress();
        Member sessionUser = sessionMap.getSessionUser();
        memberReceiveAddress.setMemberId(sessionUser.getId());
        memberReceiveAddress = BeanToUtils.dtoToDo(memberReceiveAddressAddReq, MemberReceiveAddress.class);
        return this.save(memberReceiveAddress);
    }

    @Override
    public boolean delete(Long id) {

        log.info("delete.req id={}",id);
        return this.removeById(id);
    }

    @Override
    public List<MemberReceiveAddress> listByMemberId() {

        log.info("listByMemberId");
        Member sessionUser = sessionMap.getSessionUser();
        LambdaQueryWrapper<MemberReceiveAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberReceiveAddress::getMemberId,sessionUser.getId());
        queryWrapper.orderByDesc(MemberReceiveAddress::getId);
        return this.list();
    }

    @Override
    public MemberReceiveAddress getByMemberReceiveAddressId(Long memberReceiveAddressId) {

        log.info("getByMemberReceiveAddressId.req memberReceiveAddressId={}",memberReceiveAddressId);
        MemberReceiveAddress receiveAddress = this.getById(memberReceiveAddressId);
        if (Objects.isNull(receiveAddress)){
            throw new CustomException(RespCode.FRONT_PMS_4000, MethodUtil.getLineInfo());
        }
        return receiveAddress;
    }

    @Override
    public boolean update(Long id,MemberReceiveAddressUpdReq memberReceiveAddressUpdReq) {

        log.info("update.req memberReceiveAddressAddReq={}", JSON.toJSONString(memberReceiveAddressUpdReq));
        MemberReceiveAddress memberReceiveAddress = new MemberReceiveAddress();
        Member sessionUser = sessionMap.getSessionUser();
        memberReceiveAddress.setId(id);
        memberReceiveAddress.setMemberId(sessionUser.getId());
        memberReceiveAddress = BeanToUtils.dtoToDo(memberReceiveAddressUpdReq, MemberReceiveAddress.class);
        return this.updateById(memberReceiveAddress);
    }
}
